﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.Xml.Linq;

namespace WP7EmulatorSwitcher.Model
{
    public class PhoneSkinService : IPhoneSkinService
    {
        public void GetPhoneSkins(Action<IEnumerable<PhoneSkin>, Exception> callback)
        {
            // Use this to create design time data
            
            List<PhoneSkin> skins = new List<PhoneSkin>();
            var defaultSkin = new PhoneSkin("Default");
            defaultSkin.Id = "WM7";
            defaultSkin.X = 62;
            defaultSkin.Y = 101;
            skins.Add(defaultSkin);

            var lumia800BlackSkin = new PhoneSkin("Nokia Lumia 800");
            lumia800BlackSkin.Id = "Nokia_Lumia_800_Black";
            lumia800BlackSkin.SubTitle = "Black";
            lumia800BlackSkin.X = 76;
            lumia800BlackSkin.Y = 150;
            lumia800BlackSkin.CreatorLink = "http://www.pedrolamas.com/windows-phone/nokia-lumia-800-skin/";
            lumia800BlackSkin.CreatorName = "Pedro Lamas";
            skins.Add(lumia800BlackSkin);

            var lumia800BlueSkin = new PhoneSkin("Nokia Lumia 800");
            lumia800BlueSkin.Id = "Nokia_Lumia_800_Blue";
            lumia800BlueSkin.X = 78;
            lumia800BlueSkin.Y = 150;
            lumia800BlueSkin.SubTitle = "Blue";
            lumia800BlueSkin.CreatorLink = "http://www.leitning.de/2011/nokia-lumia-800-windows-phone-emulator-skin/";
            lumia800BlueSkin.CreatorName = "Georg Kalus";
            skins.Add(lumia800BlueSkin);

            var lumia800PinkSkin = new PhoneSkin("Nokia Lumia 800");
            lumia800PinkSkin.Id = "Nokia_Lumia_800_Pink";
            lumia800PinkSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia800PinkSkin.CreatorName = "Geert van der Cruijsen";
            lumia800PinkSkin.X = 78;
            lumia800PinkSkin.Y = 150;
            lumia800PinkSkin.SubTitle = "Pink";
            skins.Add(lumia800PinkSkin);

            var lumia800WhiteSkin = new PhoneSkin("Nokia Lumia 800");
            lumia800WhiteSkin.Id = "Nokia_Lumia_800_White";
            lumia800WhiteSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia800WhiteSkin.CreatorName = "Derek Orr";
            lumia800WhiteSkin.X = 78;
            lumia800WhiteSkin.Y = 150;
            lumia800WhiteSkin.SubTitle = "White";
            skins.Add(lumia800WhiteSkin);

            var lumia710BlackBlueSkin = new PhoneSkin("Nokia Lumia 710");
            lumia710BlackBlueSkin.Id = "Nokia_Lumia_710_BlackBlue";
            lumia710BlackBlueSkin.SubTitle = "Black Blue";
            lumia710BlackBlueSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia710BlackBlueSkin.CreatorName = "Geert van der Cruijsen";
            lumia710BlackBlueSkin.X = 74;
            lumia710BlackBlueSkin.Y = 195;
            skins.Add(lumia710BlackBlueSkin);

            var lumia710BlackYellowSkin = new PhoneSkin("Nokia Lumia 710");
            lumia710BlackYellowSkin.Id = "Nokia_Lumia_710_BlackYellow";
            lumia710BlackYellowSkin.SubTitle = "Black Yellow";
            lumia710BlackYellowSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia710BlackYellowSkin.CreatorName = "Geert van der Cruijsen";
            lumia710BlackYellowSkin.X = 74;
            lumia710BlackYellowSkin.Y = 195;
            skins.Add(lumia710BlackYellowSkin);

            var lumia710WhiteBlackSkin = new PhoneSkin("Nokia Lumia 710");
            lumia710WhiteBlackSkin.Id = "Nokia_Lumia_710_WhiteBlack";
            lumia710WhiteBlackSkin.SubTitle = "White Black";
            lumia710WhiteBlackSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia710WhiteBlackSkin.CreatorName = "Geert van der Cruijsen";
            lumia710WhiteBlackSkin.X = 74;
            lumia710WhiteBlackSkin.Y = 197;
            skins.Add(lumia710WhiteBlackSkin);

            var lumia710WhiteBlueSkin = new PhoneSkin("Nokia Lumia 710");
            lumia710WhiteBlueSkin.Id = "Nokia_Lumia_710_WhiteBlue";
            lumia710WhiteBlueSkin.SubTitle = "White Blue";
            lumia710WhiteBlueSkin.CreatorLink = "http://www.vdcruijsen.net";
            lumia710WhiteBlueSkin.CreatorName = "Geert van der Cruijsen";
            lumia710WhiteBlueSkin.X = 73;
            lumia710WhiteBlueSkin.Y = 195;
            skins.Add(lumia710WhiteBlueSkin);

            var lumia710WhitePinkSkin = new PhoneSkin("Nokia Lumia 710");
            lumia710WhitePinkSkin.Id = "Nokia_Lumia_710_WhitePink";
            lumia710WhitePinkSkin.SubTitle = "White Pink";
            lumia710WhitePinkSkin.CreatorLink = "http://www.leitning.de/2011/nokia-lumia-800-windows-phone-emulator-skin/";
            lumia710WhitePinkSkin.CreatorName = "Georg Kalus";
            lumia710WhitePinkSkin.X = 75;
            lumia710WhitePinkSkin.Y = 190;
            skins.Add(lumia710WhitePinkSkin);

            var lumia900BlackSkin = new PhoneSkin("Nokia Lumia 900");
            lumia900BlackSkin.Id = "Lumia900_Black";
            lumia900BlackSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia900BlackSkin.CreatorName = "Sanjay";
            lumia900BlackSkin.X = 54;
            lumia900BlackSkin.Y = 148;
            lumia900BlackSkin.SubTitle = "Black";
            skins.Add(lumia900BlackSkin);

            var lumia900BlueSkin = new PhoneSkin("Nokia Lumia 900");
            lumia900BlueSkin.Id = "Lumia900_Blue";
            lumia900BlueSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia900BlueSkin.CreatorName = "Sanjay";
            lumia900BlueSkin.X = 54;
            lumia900BlueSkin.Y = 148;
            lumia900BlueSkin.SubTitle = "Blue";
            skins.Add(lumia900BlueSkin);

            var lumia900WhiteSkin = new PhoneSkin("Nokia Lumia 900");
            lumia900WhiteSkin.Id = "Lumia900_White";
            lumia900WhiteSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia900WhiteSkin.CreatorName = "Sanjay";
            lumia900WhiteSkin.X = 54;
            lumia900WhiteSkin.Y = 148;
            lumia900WhiteSkin.SubTitle = "White";
            skins.Add(lumia900WhiteSkin);

            var lumia610BlackSkin = new PhoneSkin("Nokia Lumia 610");
            lumia610BlackSkin.Id = "Lumia610_Black";
            lumia610BlackSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia610BlackSkin.CreatorName = "Sanjay";
            lumia610BlackSkin.X = 61;
            lumia610BlackSkin.Y = 134;
            lumia610BlackSkin.SubTitle = "Black";
            skins.Add(lumia610BlackSkin);

            var lumia610CyanSkin = new PhoneSkin("Nokia Lumia 610");
            lumia610CyanSkin.Id = "Lumia610_Cyan";
            lumia610CyanSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia610CyanSkin.CreatorName = "Sanjay";
            lumia610CyanSkin.X = 61;
            lumia610CyanSkin.Y = 134;
            lumia610CyanSkin.SubTitle = "Cyan";
            skins.Add(lumia610CyanSkin);

            var lumia610WhiteSkin = new PhoneSkin("Nokia Lumia 610");
            lumia610WhiteSkin.Id = "Lumia610_White";
            lumia610WhiteSkin.CreatorLink = "http://sharpsnippets.wordpress.com/";
            lumia610WhiteSkin.CreatorName = "Sanjay";
            lumia610WhiteSkin.X = 61;
            lumia610WhiteSkin.Y = 134;
            lumia610WhiteSkin.SubTitle = "White";
            skins.Add(lumia610WhiteSkin);

            var win8Skin = new PhoneSkin("Windows 8");
            win8Skin.Id = "WP8";
            win8Skin.CreatorLink = "http://www.leitning.de";
            win8Skin.CreatorName = "Georg Kalus";
            win8Skin.X = 67;
            win8Skin.Y = 250;
            win8Skin.SubTitle = "";
            skins.Add(win8Skin);

            var htcTitanSkin = new PhoneSkin("HTC Titan");
            htcTitanSkin.Id = "HTC_Titan";
            htcTitanSkin.X = 50;
            htcTitanSkin.Y = 115;
            htcTitanSkin.CreatorLink = "http://www.vdcruijsen.net";
            htcTitanSkin.CreatorName = "Geert van der Cruijsen";
            skins.Add(htcTitanSkin);

            var htcRadarSkin = new PhoneSkin("HTC Radar");
            htcRadarSkin.SubTitle = "Silver";
            htcRadarSkin.Id = "HTC_Radar_Silver";
            htcRadarSkin.X = 60;
            htcRadarSkin.Y = 144;
            htcRadarSkin.CreatorLink = "http://www.vdcruijsen.net";
            htcRadarSkin.CreatorName = "Geert van der Cruijsen";
            skins.Add(htcRadarSkin);

            var HtcTrophy = new PhoneSkin("HTC Trophy");
            HtcTrophy.Id = "HTC_Trophy";
            HtcTrophy.X = 62;
            HtcTrophy.Y = 130;
            HtcTrophy.CreatorLink = "http://www.vdcruijsen.net";
            HtcTrophy.CreatorName = "Geert van der Cruijsen";
            skins.Add(HtcTrophy);

            var HtcMozart = new PhoneSkin("HTC Mozart");
            HtcMozart.Id = "HTC_Mozart";
            HtcMozart.X = 61;
            HtcMozart.Y = 169;
            HtcMozart.CreatorLink = "http://arkcore.wordpress.com/2011/04/28/windows-phone-7-emulator-htc-mozart-skin/";
            HtcMozart.CreatorName = "Danijel Malik";
            skins.Add(HtcMozart);

            var HtcHd7 = new PhoneSkin("HTC HD7");
            HtcHd7.Id = "HTC_HD7";
            HtcHd7.X = 42;
            HtcHd7.Y = 72;
            HtcHd7.CreatorLink = "http://www.vdcruijsen.net";
            HtcHd7.CreatorName = "Geert van der Cruijsen";
            skins.Add(HtcHd7);

            var SamsungFocusS = new PhoneSkin("Samsung Focus S");
            SamsungFocusS.Id = "Samsung_FocusS";
            SamsungFocusS.X = 54;
            SamsungFocusS.Y = 133;
            SamsungFocusS.CreatorLink = "http://www.vdcruijsen.net";
            SamsungFocusS.CreatorName = "Geert van der Cruijsen";
            skins.Add(SamsungFocusS);

            var SamsungOmniaW = new PhoneSkin("Samsung Omnia W");
            SamsungOmniaW.Id = "Samsung_OmniaW";
            SamsungOmniaW.X = 68;
            SamsungOmniaW.Y = 170;
            SamsungOmniaW.CreatorLink = "http://www.vdcruijsen.net";
            SamsungOmniaW.CreatorName = "Geert van der Cruijsen";
            skins.Add(SamsungOmniaW);

            var SamsungOmnia7 = new PhoneSkin("Samsung Omnia 7");
            SamsungOmnia7.Id = "Samsung_Omnia7";
            SamsungOmnia7.X = 53;
            SamsungOmnia7.Y = 133;
            SamsungOmnia7.CreatorLink = "http://www.vdcruijsen.net";
            SamsungOmnia7.CreatorName = "Geert van der Cruijsen";
            skins.Add(SamsungOmnia7);

            var SamsungFocus = new PhoneSkin("Samsung Focus");
            SamsungFocus.Id = "Samsung_Focus";
            SamsungFocus.X = 79;
            SamsungFocus.Y = 201;
            SamsungFocus.CreatorLink = "http://www.jeff.wilcox.name/";
            SamsungFocus.CreatorName = "Jeff Wilcox";
            skins.Add(SamsungFocus);

            var HtcArive = new PhoneSkin("HTC Arrive");
            HtcArive.Id = "HTC_Arrive";
            HtcArive.X = 58;
            HtcArive.Y = 136;
            HtcArive.CreatorLink = "http://www.jeffblankenburg.com/2011/12/17/want-to-change-your-windows-phone-emulator/";
            HtcArive.CreatorName = "Jeff Blankenburg";
            skins.Add(HtcArive);

            var Generic = new PhoneSkin("Generic");
            Generic.Id = "Generic";
            Generic.X = 35;
            Generic.Y = 129;
            Generic.CreatorLink = "http://www.jeff.wilcox.name/";
            Generic.CreatorName = "Jeff Wilcox";
            skins.Add(Generic);

            var FujitsuIs12TGreen = new PhoneSkin("Fujitsu IS12T");
            FujitsuIs12TGreen.SubTitle = "Green";
            FujitsuIs12TGreen.Id = "Fujitsu_Is12T_Green";
            FujitsuIs12TGreen.X = 56;
            FujitsuIs12TGreen.Y = 144;
            FujitsuIs12TGreen.CreatorLink = "http://www.vdcruijsen.net";
            FujitsuIs12TGreen.CreatorName = "Geert van der Cruijsen";
            skins.Add(FujitsuIs12TGreen);

            var FujitsuIs12TBlack = new PhoneSkin("Fujitsu IS12T");
            FujitsuIs12TBlack.SubTitle = "Black";
            FujitsuIs12TBlack.Id = "Fujitsu_Is12T_Black";
            FujitsuIs12TBlack.X = 57;
            FujitsuIs12TBlack.Y = 153;
            FujitsuIs12TBlack.CreatorLink = "http://www.vdcruijsen.net";
            FujitsuIs12TBlack.CreatorName = "Geert van der Cruijsen";
            skins.Add(FujitsuIs12TBlack);

            var ZteTania = new PhoneSkin("ZTE Tania");
            ZteTania.Id = "ZTE_Tania";
            ZteTania.X = 55;
            ZteTania.Y = 138;
            ZteTania.CreatorLink = "http://www.vdcruijsen.net";
            ZteTania.CreatorName = "Geert van der Cruijsen";
            skins.Add(ZteTania);

            var Zune = new PhoneSkin("Zune HD");
            Zune.Id = "Zune";
            Zune.X = 60;
            Zune.Y = 64;
            Zune.CreatorLink = "http://www.vdcruijsen.net";
            Zune.CreatorName = "Ben Pinkerton";
            skins.Add(Zune);

            callback(skins.OrderBy(s => s.Title), null);
        }


        public bool SetEmulatorPhoneSkin(PhoneSkin skin)
        {
            
            XmlDocument xdoc = new XmlDocument();

            string programfiles = "";
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                programfiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }
            else
            {
                programfiles = Environment.GetEnvironmentVariable("ProgramFiles");
            }

            xdoc.Load(programfiles + "\\Microsoft XDE\\1.0\\WM7_Skin.xml");
            XmlNode view = xdoc.DocumentElement.SelectSingleNode("view");
            view.Attributes["downImage"].Value = skin.Id + "_Skin_Down.png";
            view.Attributes["normalImage"].Value = skin.Id + "_Skin_Up.png";
            view.Attributes["mappingImage"].Value = skin.Id + "_Skin_Mask.png";
            view.Attributes["displayPosX"].Value = skin.X.ToString();
            view.Attributes["displayPosY"].Value = skin.Y.ToString();

            XmlNodeList buttons = view.SelectNodes("button");
            for (int i = buttons.Count - 1; i >= 0; i--)
            {
                view.RemoveChild(buttons[i]);
            }

            //<button mappingColor="0xF7941D" onPressAndHold="0x3B" toolTip="Back"/> 
            XmlNode button1 = xdoc.CreateElement("button");
            ((XmlElement)button1).SetAttribute("mappingColor", "0xF7941D");
            ((XmlElement)button1).SetAttribute("onPressAndHold", "0x3B");
            ((XmlElement)button1).SetAttribute("toolTip", "Back");
            view.AppendChild(button1);

            //<button mappingColor="0x66CC66" onPressAndHold="0x3c" toolTip="Start"/> 
            XmlNode button2 = xdoc.CreateElement("button");
            ((XmlElement)button2).SetAttribute("mappingColor", "0x66CC66");
            ((XmlElement)button2).SetAttribute("onPressAndHold", "0x3c");
            ((XmlElement)button2).SetAttribute("toolTip", "Start");
            view.AppendChild(button2);

            //<button mappingColor="0xA0410D" onPressAndHold="0x3D" toolTip="Search"/>
            XmlNode button3 = xdoc.CreateElement("button");
            ((XmlElement)button3).SetAttribute("mappingColor", "0xA0410D");
            ((XmlElement)button3).SetAttribute("onPressAndHold", "0x3D");
            ((XmlElement)button3).SetAttribute("toolTip", "Search");
            view.AppendChild(button3);

            //<button mappingColor="0xA0A30D" onPressAndHold="Key_F9" toolTip="Volume Up"/>
            XmlNode button4 = xdoc.CreateElement("button");
            ((XmlElement)button4).SetAttribute("mappingColor", "0xA0A30D");
            ((XmlElement)button4).SetAttribute("onPressAndHold", "Key_F9");
            ((XmlElement)button4).SetAttribute("toolTip", "Volume Up");
            view.AppendChild(button4);

            //<button mappingColor="0xDD6500" onPressAndHold="Key_F10" toolTip="Volume Down"/>
            XmlNode button5 = xdoc.CreateElement("button");
            ((XmlElement)button5).SetAttribute("mappingColor", "0xDD6500");
            ((XmlElement)button5).SetAttribute("onPressAndHold", "Key_F10");
            ((XmlElement)button5).SetAttribute("toolTip", "Volume Down");
            view.AppendChild(button5);

            //<button mappingColor="0x3EAA00" onPressAndHold="SHUTDOWN" toolTip="Power"/> 
            XmlNode button6 = xdoc.CreateElement("button");
            ((XmlElement)button6).SetAttribute("mappingColor", "0x3EAA00");
            ((XmlElement)button6).SetAttribute("onPressAndHold", "SHUTDOWN");
            ((XmlElement)button6).SetAttribute("toolTip", "Power");
            view.AppendChild(button6);

            //<button mappingColor="0x3EAAC6" onPressAndHold="Key_F7" toolTip="Camera"/>
            XmlNode button7 = xdoc.CreateElement("button");
            ((XmlElement)button7).SetAttribute("mappingColor", "0x3EAAC6");
            ((XmlElement)button7).SetAttribute("onPressAndHold", "Key_F7");
            ((XmlElement)button7).SetAttribute("toolTip", "Camera");
            view.AppendChild(button7);

            xdoc.Save(programfiles + "\\Microsoft XDE\\1.0\\WM7_Skin.xml");
            return true;
        }
    }
}